// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env:"coursetest-4gdpfc295ac4eaea"
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  try {
    switch (event.type) {
      case "addgoods":
        const ret = await db.collection('goods').add({
          // data 字段表示需新增的 JSON 数据
          data: {
            description: "learn cloud database",
            due: new Date("2018-09-01"),
            tags: [
              "cloud",
              "database"
            ],
            location: new db.Geo.Point(113, 23),
            done: false
          }
        })
  
        return {ret}
    
      default:
        return {msg:"不能识别的操作"};
    }
  } catch (err) {
    return {err}
  }
}